/**
 * Created by udi on 2016/11/28.
 */


require(["jv", "vue", "mui", "pop"], function () {
  var Vue = require("vue");

  Vue.config.ignoredElements = ["kv", "k", "v"];

  if (!location.map.token) {
    jv.setToken();
  }

  var skip = 0, take = 5;

  var PaymentStatusEnum = jv.enumer({
    unpaid: "未支付",
    paid: "已支付"
  });

  var vm = jv.vm = new Vue({
    el: '#app',
    data: {items: [], loading: true},
    computed: {},
    methods: {
      getIfUnpaid: function(item){
        return item.payment_status == "unpaid";
      },
      getPaymentStatus: function (status) {
        return PaymentStatusEnum.getData(status);
      },
      pay: function (ev) {
        if (window.android && window.android.pay) {
          window.android.pay(this.id);
        }
        else {
          alert("找不到支付接口！");
        }
      },
      cancel: function (ev) {
        var self = this;
        var btnArray = ['否', '是'];
        mui.confirm('确认要取消该订单吗？', "", btnArray, function (e) {
          if (!e.index) {
            return;
          }
          jv.post("/wuye/order/setCancel", {orderId: self.id}, function (res) {
            jv.vm.$data.status = "cancelled";
          });
        });
      },
      getTime: function (dt) {
        dt = new Date(dt);
        return dt.toString("yyyy-MM-dd HH:mm");
      }
    }
  });


  var bindVue = function (data) {
    skip += data.length;
    data.splice(0, 0, jv.vm.$data.items.length);
    data.splice(1, 0, 0);
    jv.vm.$data.items.splice.apply(jv.vm.$data.items, data);

    if( jv.vm.$data.items.length < take){
      jv.vm.$data.loading = false;
    }
  }

  jv.scroll2Bottom(function () {
    jv.post("/fee/list", {skip: skip, take: take}, bindVue);
  }, true);
});
